home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / what's new / sample code / graphics 2d / simple drawsprocket / simple drawsprocket main.c < prev    next >
Encoding:
Text File  |  1999-10-24  |  14.0 KB  |  586 lines

  1. /*
  2.     File:        Simple DrawSprocket Main.c
  3.  
  4.     Contains:    Template for standard Macintosh Application using DrawSprocket
  5.                 Cmd-Q to exit.
  6.  
  7.     Written by:    Geoff Stahl (ggs)
  8.  
  9.     Copyright:    Copyright (c) 1999 Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19. */
  20.  
  21. // Building with DrawSprocket 1.7
  22. //#define kDSp17 1
  23.  
  24. // system includes ----------------------------------------------------------
  25.  
  26. #include <MacMemory.h>
  27. #include <Events.h>
  28. #include <Fonts.h>
  29. #include <Windows.h>
  30. #include <TextEdit.h>
  31. #include <Dialogs.h>
  32. #include <Sound.h>
  33. #include <SoundInput.h>
  34. #include <ToolUtils.h>
  35. #include <OSUtils.h>
  36. #include <Devices.h>
  37. #include <Resources.h>
  38. #include <Folders.h>
  39.  
  40. #include <DrawSprocket.h>
  41.  
  42. #include <stdio.h>
  43.  
  44. // project includes ---------------------------------------------------------
  45.  
  46.  
  47. // function declarations ----------------------------------------------------
  48.  
  49. void InitToolbox(void);
  50. void GraphicsInitAttributes(DSpContextAttributes *inAttributes);
  51. Boolean SetUp (void);
  52. void DoMenu (SInt32 menuResult);
  53. void DoKey (SInt8 theKey, SInt8 theCode);
  54. void DoUpdate (void);
  55. void CToPStr (register const char *inString, Str255 * outString );
  56. void DoEvent (void);
  57. void CleanUp (void);
  58.  
  59. // statics/globals (internal only) ------------------------------------------
  60.  
  61. // Menu defs
  62. enum 
  63. {
  64.     kMenuApple = 128,
  65.     kMenuFile = 129,
  66.     
  67.     kAppleAbout = 1,
  68.     kFileDoNothing = 1,
  69.     kFileQuit,
  70.     
  71.     kActiveSleep = 0,
  72.     
  73.     kInActiveSleep = 32767
  74. };
  75.  
  76. NumVersion gVersionDSp;
  77.  
  78. Boolean gDone = false;
  79. SInt32 gSleepTime = kActiveSleep;
  80.  
  81. RGBColor grgbColor = {0x4000,0x6000,0x6000};
  82.  
  83. DSpContextReference gTheContext;
  84. GDHandle ghGD;
  85.  
  86.  
  87. // functions (internal/private) ---------------------------------------------
  88.  
  89. void InitToolbox (void)
  90. {
  91.     MenuHandle menu;
  92.     SInt16 modifiers = 0;
  93.     EventRecord event;
  94.     short x;
  95.  
  96.     for (x = 0; x < 3; x++)
  97.         MoreMasters ();    
  98.     MaxApplZone ();
  99.  
  100.     InitGraf((Ptr) &qd.thePort);
  101.     InitFonts();
  102.     InitWindows();
  103.     InitMenus();
  104.     TEInit();
  105.     InitDialogs(nil);
  106.     InitCursor();
  107.     
  108.     qd.randSeed =  TickCount();
  109.  
  110.     // init events
  111.     EventAvail(everyEvent, &event);
  112.     modifiers |= event.modifiers;
  113.     EventAvail(everyEvent, &event);
  114.     modifiers |= event.modifiers;
  115.     EventAvail(everyEvent, &event);
  116.     modifiers |= event.modifiers;
  117.  
  118.     // Init Menus
  119.     menu = NewMenu (kMenuApple, "\p\024");            // new  apple menu
  120.     InsertMenu (menu, 0);                            // add menu to end
  121.     AppendResMenu(menu, 'DRVR');
  122.     
  123.     menu = NewMenu (kMenuFile, "\pFile");            // new menu
  124.     InsertMenu (menu, 0);                            // add menu to end
  125.     AppendMenu (menu, "\pDo Nothing;Quit/Q"); // add items
  126.     
  127.     DrawMenuBar();
  128.  
  129. // --------------------------------------------------------------------------
  130.  
  131. void GraphicsInitAttributes(DSpContextAttributes *inAttributes)
  132. {
  133.     BlockZero (inAttributes, sizeof (DSpContextAttributes));
  134. }
  135.  
  136. // --------------------------------------------------------------------------
  137.  
  138. Boolean SetUp (void)
  139. {
  140.     OSErr err = noErr;
  141.     DSpContextAttributes theAttributes;
  142.     DisplayIDType displayID;
  143.     
  144.     InitToolbox ();
  145.     
  146.     // Do DrawSprocket Setup
  147.     
  148.     if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpStartup)
  149.     {
  150.         DebugStr("\pDSp extension not loaded\n");
  151.         return false;
  152.     }
  153.     if (noErr != DSpStartup ())
  154.     {
  155.         DebugStr("\pUnable to startup DSp\n");
  156.         return false;
  157.     }
  158.         
  159.     // Find DSp version (just to show how)
  160. #ifdef kDSp17
  161.     if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpGetVersion) 
  162. #endif
  163.     {
  164.         // must be 1.1.4 or earlier so look at extension
  165.         short resFileSave, resFileDSp;
  166.         short vRefDSp;
  167.         long dirIDDSp;
  168.         FSSpec fsSpecDSp;
  169.         OSErr err;
  170.         Handle hVersion;
  171.         
  172.         // assume 1.0 version to start with (worst case)
  173.         gVersionDSp.majorRev = 1;
  174.         gVersionDSp.minorAndBugRev = 0;
  175.         gVersionDSp.stage = 0x80;
  176.         gVersionDSp.nonRelRev = 0;
  177.  
  178.         resFileSave = CurResFile();
  179.         SetResLoad (false);
  180.         // search application directory for debug lib
  181.         resFileDSp = OpenResFile ("\pDrawSprocketDebugLib");
  182.         err = ResError ();
  183.         if (fnfErr == err)
  184.         {
  185.             // search application directory for lib
  186.             resFileDSp = OpenResFile ("\pDrawSprocketLib");
  187.             err = ResError ();
  188.             if (fnfErr == err)
  189.             {
  190.                 // search extensions folder for debug lib
  191.                 FindFolder (kOnSystemDisk, kExtensionFolderType, kDontCreateFolder, &vRefDSp, &dirIDDSp);
  192.                 FSMakeFSSpec (vRefDSp, dirIDDSp, "\pDrawSprocketLib", &fsSpecDSp);
  193.                 resFileDSp = FSpOpenResFile (&fsSpecDSp, fsRdPerm);
  194.                 err = ResError ();
  195.                 if (fnfErr == err)
  196.                 {
  197.                     // search extensions folder directory for lib
  198.                     FSMakeFSSpec (vRefDSp, dirIDDSp, "\pDrawSprocketDebugLib", &fsSpecDSp);
  199.                     resFileDSp = FSpOpenResFile (&fsSpecDSp, fsRdPerm);
  200.                     err = ResError ();
  201.                 }
  202.             }
  203.         }
  204.         // if we have an open resource file and no error
  205.         if ((noErr == err) && (-1 != resFileDSp))
  206.         {
  207.             SetResLoad (true);
  208.             hVersion = GetResource ('vers', 1); 
  209.             err = ResError ();
  210.             if ((noErr == err) && (NULL != hVersion))
  211.             {
  212.                 gVersionDSp.majorRev = *(((unsigned char *)*hVersion) + 0);
  213.                 gVersionDSp.minorAndBugRev = *(((unsigned char *)*hVersion) + 1);
  214.                 gVersionDSp.stage = *(((unsigned char *)*hVersion) + 2);
  215.                 gVersionDSp.nonRelRev = *(((unsigned char *)*hVersion) + 3);
  216.                 ReleaseResource (hVersion);
  217.             }
  218.             UseResFile(resFileSave);
  219.             CloseResFile(resFileDSp);
  220.         }    
  221.     }
  222. #ifdef kDSp17
  223.     else
  224.         gVersionDSp = DSpGetVersion ();
  225. #endif
  226.  
  227.     GraphicsInitAttributes (&theAttributes);
  228.     
  229.     theAttributes.displayWidth            = 640;
  230.     theAttributes.displayHeight            = 480;
  231.     theAttributes.colorNeeds            = kDSpColorNeeds_Require;
  232.     theAttributes.displayDepthMask        = kDSpDepthMask_8;
  233.     theAttributes.displayBestDepth        = 8;
  234.     theAttributes.backBufferDepthMask    = kDSpDepthMask_All;            // must be specified even if only fornt buffer is needed
  235.     theAttributes.backBufferBestDepth    = 8;
  236.     theAttributes.pageCount                = 1;                            // only the front buffer is needed
  237.     // use two above if you want to swap buffers to animate
  238.     
  239.     // will display user dialog if context selection is required otherwise as find best context
  240.     err = DSpUserSelectContext(&theAttributes, 0L, nil, &gTheContext);
  241.     if (noErr != err)
  242.     {
  243.         DebugStr("\pA suitable display context could not be found.");
  244.         return false;
  245.     }
  246.  
  247.     DSpSetBlankingColor(&grgbColor);
  248.     
  249.     // get our device for future use
  250.     err =  DSpContext_GetDisplayID (gTheContext, &displayID);
  251.     if (noErr != err)
  252.     {
  253.         DebugStr("\pDSpContext_GetDisplayID () had an error.");
  254.         return false;
  255.     }
  256.  
  257.     // get GDHandle for ID'd device
  258.     err = DMGetGDeviceByDisplayID (displayID, &ghGD, false);
  259.     if (noErr != err)
  260.     {
  261.         DebugStr("\pDMGetGDeviceByDisplayID() had an error.");
  262.         return false;
  263.     }
  264.     
  265.     // reserve our context
  266.     err = DSpContext_Reserve (gTheContext, &theAttributes);
  267.     if (noErr != err)
  268.     {
  269.         DebugStr("\pUnable to create the display!");
  270.         return false;
  271.     }
  272.  
  273.     HideCursor();
  274.  
  275.     err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  276.     if (noErr != err)
  277.         DebugStr("\pUnable to fade the display!");
  278.  
  279.     // activate our context
  280.     err = DSpContext_SetState (gTheContext, kDSpContextState_Active);
  281.     if (noErr != err)
  282.     {
  283.         DSpContext_FadeGammaIn (NULL, NULL);    
  284.         DebugStr("\pUnable to set the display!");
  285.         return false;
  286.     }
  287.         
  288.     err = DSpContext_FadeGammaIn (NULL, NULL);    
  289.     if(noErr != err)
  290.         DebugStr("\pUnable to fade the display!");
  291.  
  292.     return true;
  293. }
  294.  
  295. // --------------------------------------------------------------------------
  296.  
  297. void DoMenu (SInt32 menuResult)
  298. {
  299.     SInt16 theMenu;
  300.     SInt16 theItem;
  301.     Str255 daName;
  302.     MenuRef theMenuHandle;
  303.         
  304.     theMenu = HiWord(menuResult);
  305.     theItem = LoWord(menuResult);
  306.     theMenuHandle = GetMenuHandle(theMenu);
  307.  
  308.     switch (theMenu)
  309.     {
  310.         case kMenuApple:
  311.             switch (theItem)
  312.             {
  313.                 case kAppleAbout:
  314.                     break;
  315.                 default:
  316.                     OpenDeskAcc(daName);
  317.                     break;
  318.             }
  319.             break;
  320.         case kMenuFile:
  321.             switch (theItem)
  322.             {
  323.                 case kFileDoNothing:
  324.                     break;
  325.                 case kFileQuit:
  326.                     gDone = true;
  327.                     break;
  328.             }
  329.             break;
  330.     }
  331.     HiliteMenu(0);
  332.     DrawMenuBar();
  333. }
  334.  
  335. // --------------------------------------------------------------------------
  336.  
  337. void DoKey (SInt8 theKey, SInt8 theCode)
  338. {
  339.     #pragma unused (theKey, theCode)
  340. }
  341.  
  342. //---------------------------------------------------------------------------
  343.  
  344. // Copy C string to Pascal string
  345.  
  346. void CToPStr (register const char *inString, Str255 * outString )        // copies in to out
  347. {    
  348.     register unsigned char x = 0;
  349.     do
  350.     {
  351.         *(((char*)outString) + 1 + x) = *(inString + x);
  352.         x++;
  353.     }
  354.     while ((*(inString + x) != 0)  && (x < 256));
  355.     *((char*)outString) = (char) x;                                    
  356. }
  357.  
  358. // --------------------------------------------------------------------------
  359.  
  360. void DoUpdate (void)
  361. {
  362.     OSStatus err;
  363.     CGrafPtr pCGrafFrontBuffer, pCGrafSave = NULL;
  364.     GDHandle hGDSave;
  365.     RGBColor rgbSave, rgbRed = {0x7FFF, 0x0000, 0x0000}, rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  366.     short fNum, len;
  367.     Str255 aStr = "\p";
  368.     char aChar[256];
  369.  
  370.     err = DSpContext_GetFrontBuffer (gTheContext, &pCGrafFrontBuffer);    
  371.     if (err != noErr)
  372.     {
  373.         DebugStr("\pUnable to get front buffer");
  374.         return;
  375.     }
  376.         
  377.     GetGWorld (&pCGrafSave, &hGDSave);
  378.     SetGWorld (pCGrafFrontBuffer, ghGD);
  379.     GetForeColor (&rgbSave);
  380.     
  381.     // draw here
  382.     RGBForeColor (&rgbRed);
  383.     PaintRect (&pCGrafFrontBuffer->portRect);
  384.  
  385.     RGBForeColor (&rgbWhite);
  386.  
  387.     // set text
  388.     GetFNum("\pGeneva", &fNum);
  389.     TextFont(fNum);
  390.     TextSize(12);
  391.     
  392.     // draw centered to screen
  393.     sprintf (aChar, "%d x %d", pCGrafFrontBuffer->portRect.right - pCGrafFrontBuffer->portRect.left, pCGrafFrontBuffer->portRect.bottom - pCGrafFrontBuffer->portRect.top);
  394.     CToPStr (aChar, &aStr);
  395.     len = StringWidth (aStr);
  396.      MoveTo ((short) (pCGrafFrontBuffer->portRect.left + (pCGrafFrontBuffer->portRect.right - pCGrafFrontBuffer->portRect.left) / 2 - (len / 2)), 18);
  397.     DrawString(aStr);
  398.     
  399.     RGBForeColor (&rgbSave);
  400.     SetGWorld (pCGrafSave, hGDSave);
  401.  
  402. /*    
  403.     OSErr err;
  404.     CGrafPtr pCGrafFrontBuffer, pCGrafSave = NULL;
  405.     GDHandle hGD = NULL;
  406.     RGBColor rgbRed = {0x7FFF, 0x0000, 0x0000};
  407.  
  408.     err = DSpContext_GetFrontBuffer (gTheContext, &pCGrafFrontBuffer);    
  409.     if (noErr != err)
  410.         DebugStr("\pUnable to get front buffer");
  411.         
  412.         
  413.     GetGWorld (&pCGrafSave, &hGD);
  414.     SetGWorld (pCGrafFrontBuffer, ghGD);
  415.     
  416.     // draw here
  417.     RGBForeColor (&rgbRed);
  418.     MoveTo (0, 0);
  419.     LineTo (640, 480);
  420.     MoveTo (640, 0);
  421.     LineTo (0, 480);
  422.     
  423.     SetGWorld (pCGrafSave, hGD);
  424. */
  425. }
  426.  
  427. // --------------------------------------------------------------------------
  428.  
  429. void DoEvent (void)
  430. {
  431.     OSErr err = noErr;
  432.     EventRecord theEvent;
  433.     SInt16 whatPart;
  434.     SInt32 menuResult;
  435.     WindowRef whichWindow;
  436.     SInt8 theKey;
  437.     SInt8 theCode;
  438.     Boolean fProcessed = false;
  439.     
  440.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
  441.     {
  442.         DSpProcessEvent(&theEvent, &fProcessed);
  443.         if (false == fProcessed)
  444.         {
  445.             switch (theEvent.what)
  446.             {
  447.                 case mouseDown:
  448.                     whatPart = FindWindow(theEvent.where, &whichWindow);
  449.                     switch (whatPart)
  450.                     {
  451.                         case inGoAway:
  452.                             break;
  453.                         case inMenuBar:
  454.                             DrawMenuBar();
  455.                             menuResult = MenuSelect(theEvent.where);
  456.                             if (HiWord(menuResult) != 0)
  457.                                 DoMenu(menuResult);
  458.                             break;
  459.                         case inSysWindow:
  460.                             SystemClick(&theEvent, whichWindow);
  461.                             break;
  462.                     }
  463.                     break;
  464.                 case keyDown:
  465.                 case autoKey:
  466.                     theKey = theEvent.message & charCodeMask;
  467.                     theCode = (theEvent.message & keyCodeMask) >> 8;
  468.                     if ((theEvent.modifiers & cmdKey) != 0)
  469.                     {
  470.                         menuResult = MenuKey(theKey);
  471.                         if (HiWord(menuResult) != 0)
  472.                             DoMenu (menuResult);
  473.                     }
  474.                     else
  475.                         DoKey (theKey, theCode);
  476.                     break;
  477.                 case updateEvt:
  478.                     BeginUpdate((WindowRef) theEvent.message);
  479.                     DoUpdate();
  480.                     EndUpdate((WindowRef) theEvent.message);
  481.                     break;
  482.                 case diskEvt:
  483.                     break;
  484.                 case osEvt:
  485.                     if (0x01000000 & theEvent.message)        //    Suspend/resume event
  486.                     {
  487.                         // need to handle DSp suspend resume events here
  488.                         if (0x00000001 & theEvent.message)    //    Resume
  489.                         {
  490.                             err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  491.                             if (noErr != err)
  492.                                 DebugStr("\pUnable to fade the display!");
  493.  
  494.                             // activate our context
  495.                             err = DSpContext_SetState (gTheContext, kDSpContextState_Active);
  496.                             if (noErr != err)
  497.                                 DebugStr("\pUnable to set the display!");
  498.                                 
  499.                             err = DSpContext_FadeGammaIn (NULL, NULL);    
  500.                             if(noErr != err)
  501.                                 DebugStr("\pUnable to fade the display!");
  502.                                 
  503.                             HideCursor ();
  504.                             gSleepTime = kActiveSleep;
  505.                         }
  506.                         else    // suspend
  507.                         {    
  508.                             err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  509.                             if (noErr != err)
  510.                                 DebugStr("\pUnable to fade the display!");
  511.  
  512.                             // pause our context
  513.                             err = DSpContext_SetState (gTheContext, kDSpContextState_Paused);
  514.                             if (noErr != err)
  515.                                 DebugStr("\pUnable to set the display!");
  516.                                 
  517.                             err = DSpContext_FadeGammaIn (NULL, NULL);    
  518.                             if(noErr != err)
  519.                                 DebugStr("\pUnable to fade the display!");
  520.                             
  521.                             ShowCursor ();
  522.                             gSleepTime = kInActiveSleep;
  523.                         }
  524.                     }
  525.                     break;
  526.  
  527.                 case kHighLevelEvent:
  528.                     AEProcessAppleEvent(&theEvent);
  529.                     break;
  530.             }
  531.         } // if DSp
  532.     }
  533.     else
  534.     {
  535.         // idle tasks
  536.     }
  537. }
  538.  
  539. // --------------------------------------------------------------------------
  540.  
  541. void CleanUp (void)
  542. {
  543.     OSErr err = noErr;
  544.  
  545.     err = DSpContext_FadeGammaOut (NULL, NULL);                    // remove for debug
  546.     if (noErr != err)
  547.         DebugStr("\pUnable to fade the display!");
  548.  
  549.     err = DSpContext_SetState (gTheContext, kDSpContextState_Inactive);
  550.     if (noErr != err)
  551.         DebugStr("\pUnable to set the display!");
  552.         
  553.     err = DSpContext_FadeGammaIn (NULL, NULL);    
  554.     if(noErr != err)
  555.         DebugStr("\pUnable to fade the display!");
  556.         
  557.     ShowCursor ();
  558.  
  559.     err = DSpContext_Release (gTheContext);
  560.     if (noErr != err) 
  561.         DebugStr ("\pDSpContext_Release error");
  562.         
  563.     err = DSpShutdown ();
  564.     if (noErr != err) 
  565.         DebugStr ("\pDSpShutdown error");
  566. }
  567.  
  568. // --------------------------------------------------------------------------
  569.  
  570. int main (void)
  571. {
  572.     if (SetUp ())
  573.     {
  574.         DoUpdate ();
  575.         while (!gDone) 
  576.         {
  577.             DoEvent ();
  578.         }
  579.     }
  580.     CleanUp ();
  581.     return 0;
  582. }
  583.  
  584.  
  585.